package com.sjl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjl.entity.Result;
import com.sjl.pojo.Member;
import com.sjl.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    /**
     * 去会员列表页面
     *
     * @return
     */
    @RequestMapping("/member_list")
    public String memberList(String NAME, Model model,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        List<Member> allMembers = memberService.getAllMembers(NAME,pageNum,pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allMembers);

        model.addAttribute("member_list", allMembers);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("NAME", NAME);
        return "member_list";
    }

    /**
     * 会员新增
     *
     * @return
     */
    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(Member member) {
        Result<Object> result = new Result<>();
        int i = memberService.addMember(member);
        if (i > 0) {
            result.setMsg("新增成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("新增失败！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 会员删除
     *
     * @return
     */
    @RequestMapping("/delMember")
    @ResponseBody
    public Result delMember(Integer id) {
        Result<Object> result = new Result<>();
        int i = memberService.deleteMemberById(id);
        if (i > 0) {
            result.setMsg("删除成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("删除失败！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @RequestMapping("/delAll")
    @ResponseBody
    public Result delAll(String ids) {
        Result<Object> result = new Result<>();
        if (ids != null && !ids.trim().equals("")) {
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);
            int i = memberService.deleteAll(idsList);
            if (i > 0) {
                result.setMsg("删除成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("删除失败！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请选择id！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 根据id查询会员
     *
     * @return
     */
    @RequestMapping("/getMemberById")
    @ResponseBody
    public Result getUserById(Integer id) {
        Result<Object> result = new Result<>();
        if (id != null) {
            Member member = memberService.getMemberById(id);
            if (member != null) {
                result.setData(member);
                result.setMsg("成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("没有此会员！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请选择id！");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 会员编辑
     *
     * @return
     */
    @RequestMapping("/editMember")
    @ResponseBody
    public Result editMember(Member member) {
        Result<Object> result = new Result<>();
        int i = memberService.editMember(member);
        if (i > 0) {
            result.setMsg("修改成功！");
            result.setSuccess(true);
        } else {
            result.setMsg("修改失败！");
            result.setSuccess(false);
        }
        return result;
    }
}